package com.iwuke.dance.wallet.settings;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.iwuke.dance.R;
import com.iwuke.dance.activity.BaseActivity;
import com.iwuke.dance.gadget_view.TitleBarView;
import com.iwuke.dance.http_request.Bean;
import com.iwuke.dance.http_request.PathUrl;
import com.iwuke.dance.util.PhoneMaskUtil;
import com.iwuke.dance.util.SharedPreferencesUtil;
import com.iwuke.dance.util.ToastUtil;
import com.iwuke.dance.util.Util;
import com.iwuke.dance.util.independent_edit_text.IndependentEditTextUtil;
import com.iwuke.dance.util.merchants_connect.NetworkEvent;
import com.iwuke.dance.wallet.PasswordSuccessfulActivity;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author: 魏从响
 * @date: 2025/10/9
 * 修改支付密码
 */
public class ChangePaymentPasswordActivity extends BaseActivity implements View.OnClickListener {

    private TitleBarView title_bar;
    private IndependentEditTextUtil independentEditTextUtil;
    private String password;
    private TextView tv_desc;

    /**
     * @description 网络实时监听
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(NetworkEvent networkEvent){
        BaseActivity.setConnect(ChangePaymentPasswordActivity.this,networkEvent,title_bar);
    }
    @Override
    protected int getView() {
        return R.layout.activity_change_payment_password;
    }

    @Override
    protected void init(View mView) {
        title_bar = findViewById(R.id.title_bar);
        //描述
        tv_desc = findViewById(R.id.tv_desc);

        independentEditTextUtil = new IndependentEditTextUtil(this, mView);


        title_bar.setTitleText("修改支付密码");
        title_bar.setLeftOnClick(this);

    }

    @Override
    protected void initData() {

        String type = getIntent().getStringExtra("text");

        independentEditTextUtil.setOnEditTextClick(new IndependentEditTextUtil.OnEditTextClick() {
            @Override
            public void onEditTextClick(String value) {
                //跳转设置新6位数密码页面
                if (TextUtils.isEmpty(password)){
                    if (!PhoneMaskUtil.hasThreeSameDigits(value)){
                        //密码成功
                        tv_desc.setText("确认支付密码");
                        password = value;
                        independentEditTextUtil.deleteData();//删除数据
                    }else {
                        ToastUtil.setToast(ChangePaymentPasswordActivity.this,"密码不能重复，连续的数字！");
                    }
                }else {
                    if (password.equals(value)){
                        if (type==null){//根据原来的密码修改
                            changePasswordByOld(value);
                        }else {//根据手机号修改
                            changePasswordByPhone(value);
                        }
                    }else {
                        //密码不一致，请重新输入
                        tv_desc.setText("设置6位数数字密码");
                        password = "";
                        independentEditTextUtil.deleteData();//删除数据
                        ToastUtil.setToast(ChangePaymentPasswordActivity.this,"密码不一致，请重新输入！");
                    }
                }
            }

            @Override
            public void onResendTimeClick() {
                //发送验证码请求

            }
        });
    }

    /**
     * @description 根据手机号修改
     * @param
     * @return
     * @author 魏从响
     * @time 2025/10/10 10:55
     */
    private void changePasswordByPhone(String value) {
        Map<String,Object> map = new HashMap<>();
        map.put("walletId", SharedPreferencesUtil.getInstance().getWalletId());
        map.put("newPassword",value);
        map.put("type",6);
        map.put("phone",SharedPreferencesUtil.getInstance().getPhoneNumber());

        PathUrl.changePasswordByPhone(map, new PathUrl.DataCallBack<Bean>() {
            @Override
            public void onSucess(Bean bean) {
                //密码修改成功
                Util.setIntent(ChangePaymentPasswordActivity.this, PasswordSuccessfulActivity.class,"1");
                finish();
            }

            @Override
            public void onFail(String errorCode) {
                ToastUtil.setToast(ChangePaymentPasswordActivity.this,errorCode);
            }
        });
    }

    /**
     * @description 根据原来的密码修改
     * @param
     * @return
     * @author 魏从响
     * @time 2025/10/10 10:55
     */
    private void changePasswordByOld(String value) {
        Map<String,Object> map = new HashMap<>();
        map.put("walletId", SharedPreferencesUtil.getInstance().getWalletId());
        map.put("newPassword",value);

        PathUrl.changePasswordByOld(map, new PathUrl.DataCallBack<Bean>() {
            @Override
            public void onSucess(Bean bean) {
                //密码修改成功
                Util.setIntent(ChangePaymentPasswordActivity.this, PasswordSuccessfulActivity.class,"1");
                finish();
            }

            @Override
            public void onFail(String errorCode) {
                ToastUtil.setToast(ChangePaymentPasswordActivity.this,errorCode);
            }
        });
    }


    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.img_left){
            finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (independentEditTextUtil!=null){
            independentEditTextUtil.onDestroy();
        }
        if (independentEditTextUtil!=null){
            independentEditTextUtil=null;
        }
    }


}
